package com.weiaibenpao.demo.chislim.hurui.adapte;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.maning.mnvideoplayerlibrary.player.MNViderPlayer;
import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.squareup.picasso.Picasso;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.hurui.bean.FunsBean;
import com.weiaibenpao.demo.chislim.hurui.bean.HoumBean;
import com.weiaibenpao.demo.chislim.hurui.httpClient.ApiClient;
import com.weiaibenpao.demo.chislim.hurui.httpClient.HHttpInterface;
import com.weiaibenpao.demo.chislim.map.customview.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by lenovo on 2017/4/17.
 */

public class HumorAdapter extends RecyclerView.Adapter {

    //两种布局形式 ，当position == 0 时，则为 ONE_ITEM 布局，position!= 0 时这位 TWO_ITEM 的布局
    public static final int ONE_ITEM = 1;
    public static final int TWO_ITEM = 2;


    private MyItemClick myItemClick ;

    public void setMyItemClick(MyItemClick myItemClick) {
        this.myItemClick = myItemClick;
    }

    //adapter的事件监听
    public interface MyItemClick{
        public void dianzhan(int position);
        public void clickImage(String[] imageUrl , int imgposition);
        public void clickIcon(int userId);
        public void addpinLun(int id,int position);
        public void replaypinLun(int position , int preUserid , String preUserName);
    }


    private Context mContext ;
    private List<HoumBean.DataBean.HumorlistBean> humorlistBean ;
    private List<HoumBean.DataBean.MarklistBean> humor_markLists ;
    public HHttpInterface apiStores = ApiClient.getModel().getService();

    public HumorAdapter(Context mContext  , List<HoumBean.DataBean.HumorlistBean> houmlistBeans  , List<HoumBean.DataBean.MarklistBean> humor_markLists){
        this.mContext = mContext ;
        this.humorlistBean = houmlistBeans ;
        this.humor_markLists = humor_markLists ;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return ONE_ITEM ;
        }else{
            return  TWO_ITEM ;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if(viewType == ONE_ITEM){
            View view = LayoutInflater.from(mContext).inflate(R.layout.humor_head_view , parent , false);
            holder = new OneViewHolder(view) ;
        }else{
            View view = LayoutInflater.from(mContext).inflate(R.layout.humor_list_view , parent , false);
            holder = new TwoViewHolder(view) ;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof OneViewHolder){
            OneViewHolder holder1 = (OneViewHolder) holder;
            LinearLayoutManager lm = new LinearLayoutManager(mContext , LinearLayoutManager.HORIZONTAL , false);
            holder1.tujian_rv.setLayoutManager(lm);
            HumorMarkAdapter adapter = new HumorMarkAdapter(mContext , humor_markLists);
            holder1.tujian_rv.setAdapter(adapter);
        }else if (holder instanceof TwoViewHolder){
            final TwoViewHolder holder1 = (TwoViewHolder) holder;
            if(!humorlistBean.get(position - 1).getUserIcon().equals("")) {
                Picasso.with(mContext).load(humorlistBean.get(position - 1).getUserIcon()).into(holder1.icon_user_iv);
            }
            holder1.icon_user_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(myItemClick != null){
                        myItemClick.clickIcon(humorlistBean.get(position - 1).getUserId());
                    }
                }
            });
            holder1.icon_user_name_tv.setText(humorlistBean.get(position - 1).getUserNickName());
            holder1.icon_user_time_tv.setText(humorlistBean.get(position - 1).getThemeTitleStr());
            if(humorlistBean.get(position - 1).getIsFans().equals("请关注")){
                holder1.guanzhu_tv.setText("关注");
            }else{
                holder1.guanzhu_tv.setText("已关注");
            }
            holder1.guanzhu_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("关注","这是说说的关注");
                    Call<FunsBean> call = apiStores.getFunsService(humorlistBean.get(position - 1).getUserId(),UserBean.getUserBean().userId);
                    call.enqueue(new Callback<FunsBean>() {
                        @Override
                        public void onResponse(Call<FunsBean> call, Response<FunsBean> response) {
                            if(response.isSuccessful()){
                                FunsBean bean = response.body() ;
                                if(bean.getCode() == 0){
                                    if(humorlistBean.get(position - 1).getIsFans().equals("请关注")){
                                        humorlistBean.get(position - 1).setIsFans("已关注");
                                    }else{
                                        humorlistBean.get(position - 1).setIsFans("请关注");
                                    }
                                    notifyDataSetChanged();
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<FunsBean> call, Throwable t) {

                        }
                    });
                }
            });

            holder1.icon_content_tv.setText(humorlistBean.get(position -1 ).getHumorContent());

            if(humorlistBean.get(position - 1).getHumorImgUrl() == null ||humorlistBean.get(position - 1).getHumorImgUrl().equals("")){
                holder1.image_rv.setVisibility(View.GONE);
                holder1.bigImage_iv.setVisibility(View.GONE);
                if(!humorlistBean.get(position - 1).getHumorVideoUrl().equals("")){
                    holder1.mn_videoplayer.setVisibility(View.VISIBLE);
                    initPlayer(holder1.mn_videoplayer , humorlistBean.get(position -1).getHumorVideoUrl()) ;
                }else{
                    holder1.mn_videoplayer.setVisibility(View.GONE);
                }
            }else{
                final String[] images = humorlistBean.get(position - 1).getHumorImgUrl().split(",");
                if(images.length == 1){
                    holder1.image_rv.setVisibility(View.GONE);
                    holder1.mn_videoplayer.setVisibility(View.GONE);
                    holder1.bigImage_iv.setVisibility(View.VISIBLE);
                    Picasso.with(mContext).load(humorlistBean.get(position - 1).getHumorImgUrl()).resize(450 , 450).centerCrop().into(holder1.bigImage_iv);
                }else {
                    holder1.image_rv.setVisibility(View.VISIBLE);
                    holder1.mn_videoplayer.setVisibility(View.GONE);
                    holder1.bigImage_iv.setVisibility(View.GONE);
                    HumorImageAdapter adapter = new HumorImageAdapter(mContext, images);
                    holder1.image_rv.setLayoutManager(new GridLayoutManager(mContext, 3));
                    holder1.image_rv.setAdapter(adapter);
                    adapter.setImageClick(new HumorImageAdapter.imageClick() {
                        @Override
                        public void imageClick(int position) {
                            if(myItemClick!= null){
                                myItemClick.clickImage(images , position);
                            }
                        }
                    });
                }
            }
/*这里是点赞区域*/
            if(humorlistBean.get(position - 1).getIsLike().equals("YES")){
                Picasso.with(mContext).load(R.mipmap.icon_red_heart).into(holder1.dianza_iv);
            }else{
                Picasso.with(mContext).load(R.mipmap.icon_heart).into(holder1.dianza_iv);
            }
            holder1.dianza_iv.setTag(position - 1);
            holder1.dianza_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(myItemClick != null){
                        int pos = (int) v.getTag();
                        myItemClick.dianzhan(pos);
                    }
                }
            });
            /*这里是点赞头像区域*/
            holder1.place_tv.setText(humorlistBean.get(position - 1).getHumorpalce());
            LinearLayoutManager manager = new LinearLayoutManager(mContext) ;
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder1.dian_rv.setLayoutManager(manager);
            holder1.dian_rv.setAdapter(new HumorIconAdapter(mContext , humorlistBean.get(position - 1).getPraiselist()));

            LinearLayoutManager manager1 = new LinearLayoutManager(mContext) ;

            //点击评论图标
            holder1.pinlun_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myItemClick.addpinLun(humorlistBean.get(position-1).getId(),position-1);
                }
            });


            /*这里是点评论区域*/
            holder1.pinLun_rv.setLayoutManager(manager1);
            HumorPinLunAdapter adapter = new HumorPinLunAdapter(mContext , humorlistBean.get(position -1).getCommlist()) ;
            holder1.pinLun_rv.setAdapter(adapter);
            adapter.setPinLunCallBack(new HumorPinLunAdapter.PinLunCallBack() {
                @Override
                public void ClickUserName(int userId) {
                    if(myItemClick != null){
                        myItemClick.clickIcon(userId);
                    }
                }

                @Override
                public void ClickCharContent(int userId ,String userName) {
                    if(myItemClick != null){
                        myItemClick.replaypinLun(position - 1  ,userId , userName);
                    }
                }
            });


        }
    }

    @Override
    public int getItemCount() {
        return humorlistBean.size() + 1;
    }



    //头布局 , 有一个横向的recycleView
    class OneViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tujian_rv)
        RecyclerView tujian_rv ;

        public OneViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //其他布局
    class TwoViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.icon_user_iv)
        CircleImageView icon_user_iv ;

        @BindView(R.id.icon_user_name_tv)
        TextView icon_user_name_tv ;

        @BindView(R.id.icon_user_time_tv)
        TextView icon_user_time_tv ;

        @BindView(R.id.guanzhu_tv)
        TextView guanzhu_tv ;

        @BindView(R.id.icon_content_tv)
        ExpandableTextView icon_content_tv ;
        @BindView(R.id.image_rv)
        RecyclerView image_rv ;

        @BindView(R.id.place_tv)
        TextView place_tv ;

        @BindView(R.id.dianza_iv)
        ImageView dianza_iv ;

        @BindView(R.id.pinlun_iv)
        ImageView pinlun_iv ;

        @BindView(R.id.mn_videoplayer)
        MNViderPlayer mn_videoplayer ;


        @BindView(R.id.dian_rv)
        RecyclerView dian_rv ;

        @BindView(R.id.pinLun_rv)
        RecyclerView pinLun_rv ;

        @BindView(R.id.bigImage_iv)
        ImageView bigImage_iv ;



        public TwoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private void initPlayer(MNViderPlayer mnViderPlayer , String url) {
        //初始化相关参数(必须放在Play前面)
        mnViderPlayer.setIsNeedBatteryListen(true);
        mnViderPlayer.setIsNeedNetChangeListen(true);

        //第一次进来先设置数据
        mnViderPlayer.setDataSource(url, "标题2");

        //播放完成监听
        mnViderPlayer.setOnCompletionListener(new MNViderPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
               // Log.i(TAG, "播放完成----");
            }
        });

        //网络监听
        mnViderPlayer.setOnNetChangeListener(new MNViderPlayer.OnNetChangeListener() {
            @Override
            public void onWifi(MediaPlayer mediaPlayer) {

            }

            @Override
            public void onMobile(MediaPlayer mediaPlayer) {
               // Toast.makeText(mContext, "请注意,当前网络状态切换为3G/4G网络", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNoAvailable(MediaPlayer mediaPlayer) {
                Toast.makeText(mContext, "当前网络不可用,检查网络设置", Toast.LENGTH_LONG).show();
            }
        });

    }

}
